﻿using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.Interaction;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Orm.DataEntity;

namespace RK.SerivesPlugin.CommonTest
{
    [Description("OA同步金蝶记录审批意见服务插件")]
    [HotUpdate]
    public class ServicePlugnSaveApprovalOpinion : AbstractOperationServicePlugIn
    {
        public override void BeginOperationTransaction(BeginOperationTransactionArgs e)
        {
            base.BeginOperationTransaction(e);
            //审核或者反审核
            if (this.FormOperation.OperationId == FormOperation.Operation_Audit
                || this.FormOperation.OperationId == FormOperation.Operation_UnAudit)
            {
                if (this.Context.ServiceType == WebType.WebService)
                {
                    var v = this.Option.GetVariableValue<object>("_InteractionDictData_", null);
                    Dictionary<string, RespondDataContext> valuePairs = v as Dictionary<string, RespondDataContext>;
                    if (valuePairs != null)
                    {
                        string key = valuePairs.Keys.First();
                        //OA同步传InterationFlags，开头用ApprovalOpinion:
                        if (key.ToUpper().StartsWith("APPROVALOPINION:"))
                        {
                            string yj = key.Substring("APPROVALOPINION:".Length);
                            //OA审批意见列
                            Field field = this.BusinessInfo.GetField("FOAApprovalOpinion3");
                            if (field != null && e.DataEntitys.Count() == 1)
                            {
                                //只会一个个单审批
                                DynamicObject order = e.DataEntitys.First();
                                string value = Convert.ToString(field.GetFieldValue(order));
                                field.DynamicProperty.SetValue(order, value + " " + yj);
                                var result = BusinessDataServiceHelper.Save(this.Context, order);

                            }
                        }
                    }
                }
            }
        }
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            //OA审批意见列
            Field field = this.BusinessInfo.GetField("FOAApprovalOpinion3");
            if (field != null)
            {
                if (!e.FieldKeys.Contains(field.Key))
                {
                    e.FieldKeys.Add(field.Key);

                }

            }
        }
    }
}
